// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIRefreshableInfo.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;

    using Org.OpenTrader.Framework.Interfaces;

    #endregion

    /// <summary>
    /// The ui refreshable info.
    /// </summary>
    public abstract class UIRefreshableInfo
    {
        #region Constants and Fields

        /// <summary>
        /// The locker.
        /// </summary>
        private Locker locker;

        /// <summary>
        /// The refreshedOn.
        /// </summary>
        private DateTime refreshedOn;

        /// <summary>
        /// The refreshNeeded.
        /// </summary>
        private bool refreshNeeded;

        /// <summary>
        /// The refreshPeriod.
        /// </summary>
        private TimeSpan refreshPeriod;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIRefreshableInfo"/> class.
        /// </summary>
        /// <param name="period">
        /// The period.
        /// </param>
        public UIRefreshableInfo(TimeSpan period)
        {
            this.locker = new Locker(333);
            this.refreshPeriod = period;
            this.refreshedOn = Timings.Now - this.refreshPeriod;
            this.refreshNeeded = true;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets Locker.
        /// </summary>
        public ILocker Locker
        {
            get
            {
                return this.locker;
            }
        }

        /// <summary>
        /// Gets or sets RefreshedOn.
        /// </summary>
        public DateTime RefreshedOn
        {
            get
            {
                return this.refreshedOn;
            }

            set
            {
                this.refreshedOn = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether RefreshNeeded.
        /// </summary>
        public bool RefreshNeeded
        {
            get
            {
                return this.refreshNeeded;
            }

            set
            {
                this.refreshNeeded = value;
            }
        }

        /// <summary>
        /// Gets RefreshPeriod.
        /// </summary>
        public TimeSpan RefreshPeriod
        {
            get
            {
                return this.refreshPeriod;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The compare to.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The compare to.
        /// </returns>
        public int CompareTo(object obj)
        {
            if (! (obj is UIRefreshableInfo))
            {
                return 0;
            }

            var delta = (obj as UIRefreshableInfo).RefreshedOn - this.RefreshedOn;
            return (int)delta.TotalMilliseconds;
        }

        /// <summary>
        /// The equals.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The equals.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (! (obj is UIRefreshableInfo))
            {
                return base.Equals(obj);
            }

            var delta = (obj as UIRefreshableInfo).RefreshedOn - this.RefreshedOn;
            return ((int)delta.TotalMilliseconds) == 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// The ui refresh.
        /// </summary>
        public abstract void UIRefresh();

        #endregion
    }
}